package com.jwd.avmanager.util;

import com.jwd.avmanager.constant.Cmd;
import com.jwd.avmanager.entity.DecodePacket;
import com.jwd.avmanager.entity.MsgPacket;
import com.jwd.avmanager.pojo.VideoServer;
import com.jwd.avmanager.pojo.WebRtc;
import com.jwd.avmanager.repository.BDMybais;

import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * @Description: 用于生成服务器响应字节数组
 * @Author: david
 * @CreateTime: 2022/4/21
 * @Company: jwd
 */
public class ResponseFactory {
    public static final int DEV_IDX = 0;
    public static final short SERIAL_NO = 0;

    /**
     * 获取心跳响应指令
     *
     * @return
     */
    public static byte[] getHeartbeatResponse() {
        MsgPacket packet = new MsgPacket(Cmd.CMD_OUT_HEARTBEAT, SERIAL_NO, DecodePacket.HEADER_LENGTH, DEV_IDX);
        return packet.getBytes();
    }

    /**
     * 登入响应指令
     */
    public static byte[] getLoginResponse(short serial_no, int dev_idx) {
        MsgPacket packet = new MsgPacket(Cmd.CMD_OUT_REGISTER_TERMINAL, serial_no, DecodePacket.HEADER_LENGTH, dev_idx);
        return packet.getBytes();
    }

    /**
     * 响应指令无特需参数
     */
    public static byte[] getResponse(short CMD, short serial_no, int dev_idx) {
        MsgPacket packet = new MsgPacket(CMD, serial_no, DecodePacket.HEADER_LENGTH, dev_idx);
        return packet.getBytes();
    }


    /**
     * 响应指令
     */
    public static byte[] getListResponse(short cmd, short serial_no, int dev_idx, byte[] bytes) throws UnsupportedEncodingException {
        MsgPacket packet = new MsgPacket(cmd, serial_no, DecodePacket.HEADER_LENGTH + bytes.length, dev_idx);
        //完善
        packet.putBody(bytes);
        DebugLogger.info(Arrays.toString(bytes) + "lie");
        return packet.getBytes();
    }

    /**
     * 列表响应指令
     *
     * @param cmd
     * @param serial_no
     * @param dev_idx
     * @param wedRTCSeva
     * @param videoServerQry
     * @param ShortAudioType
     * @param IntID
     * @param ShortFuncType
     * @return
     * @throws UnsupportedEncodingException
     */
    public static byte[] getCommandSend(short cmd, short serial_no, int dev_idx, WebRtc wedRTCSeva, String uName,VideoServer videoServerQry, byte[] ShortAudioType, byte[] IntID, byte[] ShortFuncType) throws UnsupportedEncodingException {
        DebugLogger.info(ByteUtil.bytesToShort(ShortAudioType) + "    " + ByteUtil.bytesToInt(IntID) + "    " + ByteUtil.bytesToShort(ShortFuncType) + "     " + wedRTCSeva.toString() + "   " + videoServerQry.toString());
        byte[] Audio_Port = ByteUtil.intToBytes(videoServerQry.getPort());        // 4 byte, webRTC端口号
        byte[] Audio_Add = videoServerQry.getServerIP().getBytes("GB2312");  // webRTC服务器的地址
//        wedRTCSeva.setUserName("qeqwe");
        byte[] RoomID = String.valueOf(wedRTCSeva.getRoomID()).getBytes("GB2312");      // webRTC 房间号
        byte[] UserName = (uName).getBytes("GB2312");   // 100 byte, 群组或者用户名

        byte[] bytes = new byte[260];
        System.arraycopy(ShortAudioType, 0, bytes, 0, ShortAudioType.length);
        System.arraycopy(IntID, 0, bytes, 2, IntID.length);
        System.arraycopy(ShortFuncType, 0, bytes, 6, ShortFuncType.length);
        System.arraycopy(Audio_Port, 0, bytes, 8, Audio_Port.length);
        System.arraycopy(Audio_Add, 0, bytes, 12, Audio_Add.length);
        System.arraycopy(RoomID, 0, bytes, 140, RoomID.length);
        System.arraycopy(UserName, 0, bytes, 160, UserName.length);

        MsgPacket packet = new MsgPacket(cmd, serial_no, DecodePacket.HEADER_LENGTH + bytes.length, dev_idx);
        //完善
        packet.putBody(bytes);
        {
            byte[] Audio_Port2 = new byte[4];
            byte[] Audio_Add2 = new byte[128];
            byte[] RoomID2 = new byte[20];
            byte[] UserName2 = new byte[100];

//            System.arraycopy(ShortAudioType,0,bytes,0,ShortAudioType.length);
//            System.arraycopy(IntID,0,bytes,2,IntID.length);
//            System.arraycopy(ShortFuncType,0,bytes,6,ShortFuncType.length);
            System.arraycopy(bytes, 8, Audio_Port2, 0, Audio_Port.length);
            System.arraycopy(bytes, 12, Audio_Add2, 0, Audio_Add.length);
            System.arraycopy(bytes, 140, RoomID2, 0, RoomID2.length);
            System.arraycopy(bytes, 160, UserName2, 0, UserName2.length);

            DebugLogger.info("信令头部 " + cmd + " serial_no： " + serial_no + " dev_idx: " + dev_idx);
            DebugLogger.info(ByteUtil.bytesToShort(ShortAudioType) + "    " + ByteUtil.bytesToInt(IntID) + "    " + ByteUtil.bytesToShort(ShortFuncType));
            DebugLogger.info(ByteUtil.bytesToInt(Audio_Port2));
            DebugLogger.info(new String(Audio_Add2, "GB2312").trim());
            DebugLogger.info(new String(RoomID2, "GB2312").trim());
            DebugLogger.info(new String(UserName2, "GB2312").trim());
        }
//        DebugLogger.debug(Arrays.toString(bytes)  +"lie");
        return packet.getBytes();
    }

    //通知用户的协议
    public static byte[] getInformSend(short cmd, short serial_no, int dev_idx, int  myID, WebRtc wedRTCSeva, VideoServer videoServerQry, byte[] ShortAudioType, byte[] IntID, byte[] ShortFuncType, String users) throws UnsupportedEncodingException {
        byte[] Audio_Port = ByteUtil.intToBytes(videoServerQry.getPort());        // 4 byte, webRTC端口号
        byte[] Audio_Add = videoServerQry.getServerIP().getBytes("GB2312");  // webRTC服务器的地址
        byte[] RoomID = String.valueOf(wedRTCSeva.getRoomID()).getBytes("GB2312");      // webRTC 房间号
        byte[] UserName = users.getBytes("GB2312");   // 100 byte, 群组或者用户名

        byte[] bytes = new byte[260];
        System.arraycopy(ShortAudioType, 0, bytes, 0, ShortAudioType.length);
        System.arraycopy(IntID, 0, bytes, 2, IntID.length);
        System.arraycopy(ShortFuncType, 0, bytes, 6, ShortFuncType.length);
        System.arraycopy(Audio_Port, 0, bytes, 8, Audio_Port.length);
        System.arraycopy(Audio_Add, 0, bytes, 12, Audio_Add.length);
        System.arraycopy(RoomID, 0, bytes, 140, RoomID.length);
        System.arraycopy(UserName, 0, bytes, 160, UserName.length);
        MsgPacket packet = new MsgPacket(cmd, serial_no, DecodePacket.HEADER_LENGTH + bytes.length, dev_idx);
        packet.putBody(bytes);
        {
            byte[] Audio_Port2 = new byte[4];
            byte[] Audio_Add2 = new byte[128];
            byte[] RoomID2 = new byte[20];
            byte[] UserName2 = new byte[100];

//            System.arraycopy(ShortAudioType,0,bytes,0,ShortAudioType.length);
//            System.arraycopy(IntID,0,bytes,2,IntID.length);
//            System.arraycopy(ShortFuncType,0,bytes,6,ShortFuncType.length);
            DebugLogger.info("信令头部 " + cmd + " serial_no： " + serial_no + " dev_idx: " + dev_idx);
            DebugLogger.info(ByteUtil.bytesToShort(ShortAudioType) + "    " + ByteUtil.bytesToInt(IntID) + "    " + ByteUtil.bytesToShort(ShortFuncType));
            System.arraycopy(bytes, 8, Audio_Port2, 0, Audio_Port.length);
            System.arraycopy(bytes, 12, Audio_Add2, 0, Audio_Add.length);
            System.arraycopy(bytes, 140, RoomID2, 0, RoomID2.length);
            System.arraycopy(bytes, 160, UserName2, 0, UserName2.length);
            DebugLogger.info(ByteUtil.bytesToInt(Audio_Port2));
            DebugLogger.info(new String(Audio_Add2, "GB2312").trim());
            DebugLogger.info(new String(RoomID2, "GB2312").trim());
            DebugLogger.info(new String(UserName2, "GB2312").trim());


        }
        return packet.getBytes();
    }

    public static byte[] getSend(short cmd, short serial_no, int dev_idx) throws UnsupportedEncodingException {
        MsgPacket packet = new MsgPacket(cmd, serial_no, DecodePacket.HEADER_LENGTH, dev_idx);
        //完善
        return packet.getBytes();
    }

    public static byte[] getServer(short cmd, short serial_no, int dev_idx, String ServerIP, int ServerPort) throws UnsupportedEncodingException {
        byte[] Audio_Port = ByteUtil.intToBytes(ServerPort);        // 4 byte, webRTC端口号
        byte[] Audio_Add = ServerIP.getBytes("GB2312");  // webRTC服务器的地址
        byte[] RoomID = "0".getBytes("GB2312");      // webRTC 房间号

        byte[] bytes = new byte[152];
        System.arraycopy(Audio_Port, 0, bytes, 0, Audio_Port.length);
        System.arraycopy(Audio_Add, 0, bytes, 4, Audio_Add.length);
        System.arraycopy(RoomID, 0, bytes, 132, RoomID.length);
        MsgPacket packet = new MsgPacket(cmd, serial_no, DecodePacket.HEADER_LENGTH + bytes.length, dev_idx);


        DebugLogger.info("信令头部 " + cmd + " serial_no： " + serial_no + " dev_idx: " + dev_idx);
        DebugLogger.info(ByteUtil.bytesToInt(Audio_Port));
        DebugLogger.info(new String(Audio_Add, "GB2312").trim());
        DebugLogger.info(new String(RoomID, "GB2312").trim());

        packet.putBody(bytes);
        return packet.getBytes();
    }

    public static String CrtRTMPUrl(Integer dev_idx, String rtmpStreamKey, Integer i) {
        long l = System.currentTimeMillis() / 1000 + (60 * 60 * 24);
        String MD5Pwd = "/live/" + rtmpStreamKey + dev_idx + "?sign=" + l + "-" + Cmd.UrlKey;
        String sMD5Pwd = MD5Util.getMD5Str(MD5Pwd);
//        String sUrl = "rtmp://119.23.66.152:11935" + "/live/" + rtmpStreamKey + dev_idx + "?sign=" + l + '-' + sMD5Pwd;
        BDMybais instance = BDMybais.getInstance();
        VideoServer videoServerQry = instance.getVideoServerQry(53);
        String serverIP = videoServerQry.getServerIP();
        int port = videoServerQry.getPort();
        String sUrl = "rtmp://"+serverIP+":"+ port + "/live/" + rtmpStreamKey + dev_idx + "?sign=" + l + '-' + sMD5Pwd;

        return sUrl;
    }



    /**
     * 异常响应
     */
    public static void ExceptionResponse(short cmd, byte[] bytes, ChannelHandlerContext ctx, Logger logger) throws UnsupportedEncodingException {
        MsgPacket packet = new MsgPacket(cmd, (short) 0, DecodePacket.HEADER_LENGTH + bytes.length, 0);
        //完善
        packet.putBody(bytes);
        DebugLogger.err(logger,Arrays.toString(bytes) + "lie");

        ResponseSendUtil.writeAndFlush(packet.getBytes(), ctx);
        DebugLogger.err(logger,"异常响应发送成功");
    }

    public static byte[] httpUplod(short cmd, short serial_no, Integer dev_idx, String url) throws UnsupportedEncodingException {
        byte[] urlByte = url.getBytes("GB2312");        // 请求路径

        byte[] bytes = new byte[80];
        System.arraycopy(urlByte, 0, bytes, 0, urlByte.length);

        MsgPacket packet = new MsgPacket(cmd, serial_no, DecodePacket.HEADER_LENGTH + bytes.length, dev_idx);



        packet.putBody(bytes);
        return packet.getBytes();
    }
}
